<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/24
 * Time: 18:19
 */

namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Request;
use Redis\Redis;

class GetInfo extends Controller
{
    /**
 * Home constructor.
 * @param Request|null $request
 * @content 构造方法
 */
    function __construct(Request $request = null)
    {
        parent::__construct($request);
    }
    /**
     * @param phone :手机号
     * @content 发送短信验证码
     */
    function getSmsCode(){
        $phone = input("post.phone");
        # 接收操作标识
        $mark = input('post.mark');
        #判断下手机号是否存在
        $resPhone = Db::table('member')->field('id')->where(" phone = $phone and member_type !=0")->find();
        if ($mark == 'registerPhone') {
            # 注册发送验证码
            if (!empty($resPhone))
                return array('status' => false, 'msg' => '该手机号已注册');
        }else if($mark == 'writephone'){
            # 修改手机号时发送验证码
            if (!empty($resPhone))
                return array('status' => false, 'msg' => '该手机号已注册,请更换手机号修改!');
        }
        $sms = new SmsCode();
        #把验证码存一下redis
        $redis = new Redis();

        # 从redis中获取改手机号是否存在验证码
        $codeNum = $redis->hGet('SmsCode', $phone);
        if ($codeNum) {
            # 存在验证码
            $code = $sms->Ysms($phone, $codeNum);
            if (!empty($code) and $code['status'] == true) {
                return array('status' => true, 'num' => $code['code'], 'msg' => '验证码发送成功');
            } else {
                return array('status' => false, 'msg' => '验证码发送失败');
            }
        } else {
            $code = $sms->Ysms($phone, randCodes());
            if (!empty($code) and $code['status'] == true) {
                $redis->hSet('SmsCode', $phone, $code['code'], '1800');
                return array('status' => true, 'num' => $code['code'], 'msg' => '验证码发送成功');
            } else {
                return array('status' => false, 'msg' => '验证码发送失败');
            }
        }
    }
}
